﻿// load the array list from the SVN trunk
// Block List
var host = 'http://hkglolblock.googlecode.com/svn/trunk/';
var LOLBlockBlockArrayLoader = document.createElement('script');
LOLBlockBlockArrayLoader.type = 'application/javascript';
LOLBlockBlockArrayLoader.id = 'LOLBlockBlockArrayLoaderScript';
LOLBlockBlockArrayLoader.src = host+'array.js';
document.getElementsByTagName('head')[0].appendChild(LOLBlockBlockArrayLoader);
// Unblock list
var LOLBlockUnblockArrayLoader = document.createElement('script');
LOLBlockUnblockArrayLoader.type = 'application/javascript';
LOLBlockUnblockArrayLoader.id = 'LOLBlockUnblockArrayLoaderScript';
LOLBlockUnblockArrayLoader.src = host+'unblock_array.js';
document.getElementsByTagName('head')[0].appendChild(LOLBlockUnblockArrayLoader);

// ...
var LOLBlock_oTitle = document.title;
var LOLBlock_runDate = new Date();
var LOLBlock_logText = '<html><head></head><body>一鍵 Block lol台主 腳本<hr width="100%"/>瀏覽器 User-Agent：' + navigator.userAgent + '<br/>執行日期及時間：' + LOLBlock_runDate.toLocaleDateString() + ' ' + LOLBlock_runDate.toLocaleTimeString() + '</br>以下為所有在腳本中指定用戶的結果： [userid, result]</br>';
var LOLBlock_Working = false;
function LOLBlock_Dispose()
{
	if(LOLBlock_Working)
	{
		LOLBlock_AwaitingToDisposeFlag = true;
		LOLBlock_RefreshBlockStatusTitle(LOLBlock_GetBlockStatusTitle().replace('執行中','正在取消，請稍候'));
		document.getElementById('LOLBlock_Container').style.cursor="wait";
	}
	else
	{
		document.title = LOLBlock_oTitle;
		document.getElementById('LOLBlock_Container').style.cursor="default";
		document.getElementsByTagName('body')[0].removeChild(document.getElementById('LOLBlockInterface'));
		var ScriptNodeToRemoveById = ['LOLBlockLoaderScript', 'LOLBlockBlockArrayLoaderScript', 'LOLBlockUnblockArrayLoaderScript'];
		for (var i = 0; i < ScriptNodeToRemoveById.length; i++)
		{
			if(document.getElementById(ScriptNodeToRemoveById[i]) != null) document.getElementById(ScriptNodeToRemoveById[i]).parentNode.removeChild(document.getElementById(ScriptNodeToRemoveById[i]));
		}
	}
}
var LOLBlockInterface = document.createElement('div');
LOLBlockInterface.id = 'LOLBlockInterface';
LOLBlockInterface.innerHTML = '<div id="LOLBlock_Background" style="width: 100%; height: 100%; position: fixed; left: 0; top: 0; background-color: #000000; opacity: 0.5; filter:alpha(opacity=0.5)"></div><div id="LOLBlock_Container" style="position: fixed; left: 50%; top: 50%; margin: -55px 0 0 -150px; padding: 10px 10px 10px 10px; width: 300px; height: 110px; background-color: #999999; border: 1px solid #333333; color: #000000; ">Loading</div>';
document.getElementsByTagName('body')[0].appendChild(LOLBlockInterface);
function LOLBlock_cOut(text)
{
	document.getElementById('LOLBlock_Container').innerHTML = text;
}
function LOLBlock_forcelog()
{
	window.open('data:text/html;charset=utf-8,'+encodeURI(LOLBlock_logText),'_blank');
	return false;
}
function LOLBlock_InitialiseBlockUI()
{
	var LOLBlock_BlockingHTML = '<div id="LOLBlock_forcelog" style="position: relative; right: 0; top: 0; float: right; font-size: 5px;"><a role="button" onclick="LOLBlock_forcelog();" onmiddleclick="LOLBlock_forcelog();">log</a></div><div id="LOLBlock_StatusTitle">準備中……</div><br/><div id="LOLBlock_StatusText">StatusText</div><br/><div style="width: 100%; height: 22px;"><div id="LOLBlock_ProgressBarBackground" style="border: 1px solid; margin: 0 60px -22px 0; height: 100%;"><div id="LOLBlock_ProgressBarForeground" style="background-color: #0000cc; width: 0; height: 100%;"></div></div>&nbsp;<button type="button" style="margin: -2px; float: right;" onclick="LOLBlock_Dispose()">取消</button></div>';
	LOLBlock_cOut(LOLBlock_BlockingHTML);
}
var LOLBlock_ConfirmHTML = '你是否確定要將所有 lol 台主 一次過 Block 清？<br />在本腳本執行期間你可以隨時終止動作。<br/><p style="text-align: center;"><button type="button" onclick="LOLBlock_InitialiseBlockUI(); LOLBlock_RefreshCountAndJumpToNext();">確定</button>&nbsp;<button onclick="LOLBlock_Dispose()">取消</button></p>';
LOLBlock_cOut(LOLBlock_ConfirmHTML);
function LOLBlock_GetBlockStatusTitle()
{
	return document.getElementById('LOLBlock_StatusTitle').innerHTML;
}
function LOLBlock_RefreshBlockStatusTitle(text)
{
	document.getElementById('LOLBlock_StatusTitle').innerHTML = text;
}
function LOLBlock_RefreshWorkStatusText(text)
{
	document.getElementById('LOLBlock_StatusText').innerHTML = text;
}
function LOLBlock_RefreshBlockStatusPercentage(percentage)
{
	document.getElementById('LOLBlock_ProgressBarForeground').style.width = Math.round(percentage * 100) + "%";
}
var LOLBlock_AwaitingToDisposeFlag = false;
var LOLBlock_ThisWorkStartTime = new Date();
var LOLBlock_UnblockingIndex = -1;
var LOLBlock_BlockingIndex = -1;
var LOLBlock_WorkingIdAsText = '';
var LOLBlock_SuccessBlockCount = 0;
var LOLBlock_AlreadyBlockedCount = 0;
var LOLBlock_ErrorBlockCount = 0;
var LOLBlock_BlockResult = [];
var LOLBlock_BlockErrorText = [];
function LOLBlock_cOutCostTime()
{
	var lastMs = (LOLBlock_ThisWorkStartTime.getMinutes() * 60 + LOLBlock_ThisWorkStartTime.getSeconds() * 1000) + LOLBlock_ThisWorkStartTime.getMilliseconds();
	var d = new Date();
	var thisMs = (d.getMinutes() * 60 + d.getSeconds() * 1000) + d.getMilliseconds();
	return thisMs - lastMs;
}

// unblock
function LOLBlock_DoUnblockNextUserId()
{
	if(LOLBlock_AwaitingToDisposeFlag)
	{
		LOLBlock_Dispose();
	}
	else
	{
		LOLBlock_Working = true;
		LOLBlock_UnblockingIndex++;
		LOLBlock_WorkingIdAsText = LOLBlock_UnblockUserId[LOLBlock_UnblockingIndex];
		LOLBlock_logText +=  '-' + LOLBlock_WorkingIdAsText + ': ';
		LOLBlock_RefreshWorkStatusText('-' + LOLBlock_WorkingIdAsText + ': ' + '正等候伺服器回應中……');
		
		if(LOLBlock_HelianthusAnnuus_Exist())
		{
			LOLBlock_HelianthusAnnuus_SwitchBam(LOLBlock_UserId[LOLBlock_BlockingIndex], true);
		}
		MessageFunc.RemoveBlockUser(LOLBlock_UnblockUserId[LOLBlock_UnblockingIndex], LOLBlock_UnblockGoodResult, LOLBlock_UnblockBadResult);
		LOLBlock_ThisWorkStartTime = new Date();
	}
}
function LOLBlock_UnblockGoodResult(result)
{
	LOLBlock_logText += '(' + LOLBlock_cOutCostTime() + 'ms) '
	LOLBlock_Working = false;
	// unblock does not give a length of >0 result if such user has already unblocked
	if (result.errMsg != '')
	{
		LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': 錯誤：發生伺服器錯誤。請屌 @min。');
		LOLBlock_ErrorBlockCount++;
		LOLBlock_logText += '錯誤："' + result.errMsg + '"</br>';
	}
	else
	{
		LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': ' + '已成功 unblock 此 user！');
		LOLBlock_SuccessBlockCount++;
		LOLBlock_logText += '早已解除/成功<br/>';
	}
	LOLBlock_RefreshCountAndJumpToNext();
}
function LOLBlock_UnblockBadResult(result)
{
	LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': 錯誤：發生伺服器錯誤。請屌 @min。(' +  + ')');
	LOLBlock_ErrorBlockCount++;
	LOLBlock_logText += '錯誤："' + result.errMsg + '"</br>';
	LOLBlock_RefreshCountAndJumpToNext();
}

// block
function LOLBlock_DoBlockNextUserId()
{
	if(LOLBlock_AwaitingToDisposeFlag)
	{
		LOLBlock_Dispose();
	}
	else
	{
		LOLBlock_Working = true;
		LOLBlock_BlockingIndex++;
		LOLBlock_WorkingIdAsText = LOLBlock_UserId[LOLBlock_BlockingIndex];
		LOLBlock_logText += '+' + LOLBlock_WorkingIdAsText + ': ';
		LOLBlock_RefreshWorkStatusText('+' + LOLBlock_WorkingIdAsText + ': ' + '正等候伺服器回應中……');
		
		if(LOLBlock_HelianthusAnnuus_Exist())
		{
			LOLBlock_HelianthusAnnuus_SwitchBam(LOLBlock_UserId[LOLBlock_BlockingIndex], true);
		}
		MessageFunc.BlockUser(LOLBlock_UserId[LOLBlock_BlockingIndex], LOLBlock_BlockGoodResult, LOLBlock_BlockBadResult);
		LOLBlock_ThisWorkStartTime = new Date();
	}
}
function LOLBlock_BlockGoodResult(result)
{
	/*if (result.errMsg != "") {
		document.getElementById('bottomFunc').innerHTML = result.errMsg;
	} else {
		document.getElementById('bottomFunc').innerHTML = '';
	        blocked_list = result.list;
	        CheckBlockedUser();
	}*/
	LOLBlock_logText += '(' + LOLBlock_cOutCostTime() + 'ms) '
	LOLBlock_Working = false;
	if (result.errMsg == 'The user is already blocked before.')
	{
		LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': ' + '該用戶早已在閣下封鎖列表中。');
		LOLBlock_AlreadyBlockedCount++;
		LOLBlock_logText += '已存在於列表</br>';
	}
	else if (result.errMsg != '')
	{
		LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': 錯誤：發生伺服器錯誤。請屌 @min。');
		LOLBlock_ErrorBlockCount++;
		LOLBlock_logText += '錯誤："' + result.errMsg + '"</br>';
	}
	else
	{
		LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': ' + '已成功 block 此 user！');
		LOLBlock_SuccessBlockCount++;
		LOLBlock_logText += '成功<br/>';
	}
	LOLBlock_RefreshCountAndJumpToNext();
}
function LOLBlock_BlockBadResult(result)
{
	LOLBlock_RefreshWorkStatusText(LOLBlock_WorkingIdAsText + ': 錯誤：發生伺服器錯誤。請屌 @min。(' +  + ')');
	LOLBlock_ErrorBlockCount++;
	LOLBlock_logText += '錯誤："' + result.errMsg + '"</br>';
	LOLBlock_RefreshCountAndJumpToNext();
}

// Helianthus-Annuus Support 
function LOLBlock_HelianthusAnnuus_Exist()
{
	if(typeof AN !== 'undefined')
		if(AN.util.data('aBamList') != null) return true;
	
	return false;
}
function LOLBlock_HelianthusAnnuus_SwitchBam(userid, toBam)
{
	var bamList = [];
	if (typeof AN.util.data('aBamList') !== 'undefined' && AN.util.data('aBamList') != null && AN.util.data('aBamList').length > 0)
	{
		bamList = AN.util.data('aBamList'); // JS can't access into jQ's function -- access the real object instead
	}
	var index = bamList.indexOf(userid); // ~= var index = $.inArray(userid, bamList); in jQuery ---- You know I won't play jQ trick this time
	if(index === -1)
	{
		if(toBam)
		{
			bamList.push(userid);
		}
	}
	else
	{
		if(!toBam)
		{
			bamList.splice(index, 1);
		}
	}
	
	AN.util.data('aBamList', bamList); // jQ core func
//	toggleReplies(null); // can't, jQ func
}

// jump to next userid
function LOLBlock_RefreshCountAndJumpToNext()
{
	var ubI = LOLBlock_UnblockingIndex;
	var bI = LOLBlock_BlockingIndex;
	if(LOLBlock_UnblockingIndex < 0)
	{
		ubI = 0;
	}
	if(LOLBlock_BlockingIndex < 0)
	{
		bI = 0;
	}
	var percentage = (ubI + bI) / (LOLBlock_UnblockUserId.length + LOLBlock_UserId.length);
	document.title = 'Blocking lol: ' + Math.round(percentage * 100) + '%';
	LOLBlock_RefreshBlockStatusPercentage(percentage);
	LOLBlock_RefreshBlockStatusTitle('執行中……(' + Math.round(percentage * 100) + '%)');
	if (LOLBlock_UnblockingIndex + 1 < LOLBlock_UnblockUserId.length)
	{
		LOLBlock_DoUnblockNextUserId();
	}
	else if (LOLBlock_BlockingIndex + 1 < LOLBlock_UserId.length)
	{
		LOLBlock_DoBlockNextUserId();
	}
	else
	{
		LOLBlock_UnblockingIndex = -1;
		LOLBlock_BlockingIndex = -1;
		LOLBlock_BlockFinished();
	}
}

// finishing
function LOLBlock_BlockFinished()
{
	LOLBlock_logText += '<br/>完成日期及時間：' + LOLBlock_runDate.toLocaleDateString() + ' ' + LOLBlock_runDate.toLocaleTimeString() + '<hr width="100%"/></body></html>';
	var LOLBlock_FinishHTML = '完成！<br/><br/>總數 ' + (LOLBlock_UnblockUserId.length + LOLBlock_UserId.length) + ' | 成功 ' + LOLBlock_SuccessBlockCount + ' | 錯誤 ' + LOLBlock_ErrorBlockCount + ' | 已存在 ' + LOLBlock_AlreadyBlockedCount + '<br /><a href="data:text/html;charset=utf-8,' + encodeURI(LOLBlock_logText) + '" target="_blank" download="LOLBlock_log.htm">如需詳細記錄請按此開新頁查看或儲存。</a><br /><div style="text-align: center;"><button type="button" onClick="LOLBlock_Dispose()">關閉</button>';
	LOLBlock_cOut(LOLBlock_FinishHTML);
}